package com;

import freemarker.template.TemplateException;



import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

//主方法入口
public class Main {
    public static void main(String[] args) throws IOException, TemplateException {
        if (args.length != 0) {
            String packagepath = null;
            String moban = null;
            String htmlname = null;
            for (String k : args) {
                if (k.equals("-help")) {
                    System.out.println("-p  Package path");
                    System.out.println("-m  Template type");
                    System.out.println("    Template type:");
                    System.out.println("See the config folder for configuration details:/lustre/sdb/xueyj/htmlScript/config");
                    Properties properties = new Properties();
                    /*//properties.load(new FileReader("E:\\projects\\GenHtml2.0\\src\\main\\resources\\mobantype.properties"));*/
                    properties.load(new FileReader("/lustre/sdb/xueyj/htmlScript/config/mobantype.properties"));
                    for (String a : properties.stringPropertyNames()) {
                        System.out.println("        " + properties.get(a));
                    }
                    System.out.println("-a  Generate page name by default(genome)");
                    //System.out.println("-v  查看详细配置信息位置");
                }
                if (k.substring(0, 3).equals("-m=")) {
                    moban = k.substring(3);
                }
                if (k.substring(0, 3).equals("-p=")) {
                    packagepath = k.substring(3);
                }
                if (k.substring(0, 3).equals("-a=")) {
                    htmlname = k.substring(3);
                }
            }
            if (moban == null || packagepath == null) {
                System.out.println("Please enter the necessary parameters -m  -p ");
            } else {
                Properties pp = new Properties();
                //pp.load(new FileReader("E:\\projects\\GenHtml2.0\\src\\main\\resources\\base.properties"));
                pp.load(new FileReader("/lustre/sdb/xueyj/htmlScript/config/base.properties"));
                if (htmlname == null) {
                    htmlname = (String) pp.get("htmlname");
                }
                if (moban.equals("plant-PacBio") || moban.equals("plant-Nanopore")) {
                    moban = orfsmoban(moban, packagepath);
                }
                String mobanpath = (String) pp.get(moban);
                MakeHtml.run(packagepath, mobanpath, htmlname, moban);
            }
        } else {
            System.out.println("Please enter the necessary parameters: -help Check the parameters");
            System.out.println("Required parameters -m ; -p");
            System.out.println("Example reference path:/lustre/sdb/xueyj/htmlScript/sample");
        }

    }

    private static String orfsmoban(String moban, String packagepath) {
        String sp = File.separator;
        packagepath = packagepath.replace("/", sp);
        packagepath = packagepath.replace("\\", sp);
        File file = new File(packagepath + sp + "03.Genome_Component" + sp + "ORFs");
        if (file.exists() && file.isDirectory()) {
            return moban + "-orfs";
        } else {
            return moban;
        }
    }
}
